package com.chance.cc.crawler.prod.command.job.domain.news.guokr;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.chance.cc.crawler.prod.command.job.domain.news.NewsCommonScript;
import org.apache.commons.lang3.StringUtils;

public class GuoKrCrawlerScheduleJob extends NewsCommonScript {
    private static final String SITE = "news";//public --> private?
    private static final String START_URL = "https://www.guokr.com/";
    public static final String DOMAIN = "guokr";
    private static final String SITE_BIZ = "realtime";

    public static void main(String[] args) {
        publishGuoKrArticleCrawlerScheduleJobInfo();
    }
    public static CrawlerJob publishGuoKrArticleCrawlerScheduleJobInfo(){
        CrawlerJob crawlerJob = articleScheduler();

        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();//采集作业
        crawlerScheduleJob.setDomain(DOMAIN);//设置域
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(crawlerJob));//设置采集信息
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());//工作标签
        crawlerScheduleJob.setNote("果壳采集7天内的文章互动量和昨日最新回复");//采集提示
        crawlerScheduleJob.setCrawlerKey(crawlerJob.generateCrawlerKey());//生成爬虫key
        HttpPage httpPage = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);//发布标签命令添加和修改调度工作
        System.out.println("发布作业："+httpPage.getRawText());//打印文本
        return crawlerJob;//返回工作爬虫
    }
    public static CrawlerJob articleScheduler(){
        CrawlerRequestRecord crawlerRequestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(DOMAIN, CrawlerEnum.CrawlerRequestType.turnPage)
                .domain(DOMAIN)
                .httpUrl(START_URL)
                .recordKey(START_URL)
                .releaseTime(System.currentTimeMillis())
                //.filter(CrawlerEnum.CrawlerRecordFilter.keyOrDateRange)
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                .addFilterInfo(FilterUtils.redisFilterKeyInfo(StringUtils.joinWith("-","filter",DOMAIN,SITE,SITE_BIZ,"queue")))
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24*7,null))
                .build();
        crawlerRequestRecord.tagsCreator().bizTags().addDomain(DOMAIN);
        crawlerRequestRecord.tagsCreator().bizTags().addSite(SITE);
        crawlerRequestRecord.tagsCreator().bizTags().addSiteBiz(SITE_BIZ);

        String requestQueueName = StringUtils.joinWith("-", "crawler", DOMAIN, SITE, SITE_BIZ, "queue");
        CrawlerJob crawlerJob = CrawlerJob.builder()
                .crawlerJobThreadNumber(3)
                .triggerInfo(
                        DOMAIN,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-",SITE,SITE_BIZ,CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(requestQueueName))
                //.fileResultPipeline(null,"/data/chance_crawler_runner/logs/node/tianya.log",true)
                .kafkaResultPipeline(null,kafkaTopic,null) //测试先把它注释掉
                .requestRecord(crawlerRequestRecord)
                .build();
        return crawlerJob;
    }
}
